<?php

define('WHITEBOARD_TP_PAGE_HANDLER', 'photos');
define('WHITEBOARD_ACTION_HANDLER', 'whiteboard');
define('WHITEBOARD_PLUGIN_DIR', 'greengar/whiteboard');
define('WHITEBOARD_IMAGE_DIR', 'image');

define('WHITEBOARD_DRAWING_SUBTYPE', 'image');
define('WHITEBOARD_DRAWING_CLASS', 'WhiteboardDrawing');
define('WHITEBOARD_DRAWING_METADATA', 'whiteboarddrawing');

define('WHITEBOARD_DRAWING_LATTITUDE', 'lattitude');
define('WHITEBOARD_DRAWING_LONGTITUDE', 'longtitude');

class WhiteboardDrawing extends TidypicsImage {

    protected function initialise_attributes() {
        $this->attributes['type'] = 'object';
        $this->attributes['subtype'] = WHITEBOARD_DRAWING_SUBTYPE;
        //$this->attributes['owner_guid'] = get_loggedin_userid();
        $this->attributes['access_id'] = ACCESS_PUBLIC;
    }

    public function __contruct($guid = null) {
        parent::__construct($guid);
    }

    public function saveDrawing($attributes = array(), $metadata = array(), $annotations = array()) {
        foreach ($attributes as $k => $v)
        {
            $this->set($k, $v);
        }

        if (!parent::save()) {
            return false;
        }
        
        // Set album cover
        $wb_album = new WhiteboardAlbum($this->container_guid);
        $wb_album->setMetadata('cover', $this->guid);
        
        // Set drawing's metadata
        $this->setMetaData('guid', $this->guid);
        $this->setMetaData(WHITEBOARD_DRAWING_METADATA, 1);
        $this->setMetaData('is_hidden', 0);
        $this->setMetaData('reporters', 0);
        $this->setMetaData('avg_rating', 0);
        $this->setMetaData('rate_num', 0);
        $this->setMetaData('likeno', 0);
        foreach ($metadata as $k => $v)
            $this->setMetaData($k, $v);

        foreach ($annotations as $k => $v)
            $this->annotate($k, $v);

        return true;
    }
    
    public static function toArray($drawing) {
        $ownerEntity = get_entity($drawing->owner_guid);
        return array(
            'guid' => $drawing->getGUID(),
            'owner_guid' => $drawing->owner_guid,
            'owner_name' => $ownerEntity->name,
            'location' => 'Ho Chi Minh city',
            'comment_no' => $drawing->countAnnotations('generic_comment'),
//            'like_no' => greengar_count_favorite($drawing->getGUID(), WHITEBOARD, FALSE),
            'like_no' => $drawing->likeno ? $drawing->likeno : 0,
            'time' => $drawing->getTimeCreated(),
            'width' => 0,
            'height' => 0,
            'portrait' => 1,
            'title' => $drawing->title,
            'description' => $drawing->description,
            'tags' => $drawing->getMetaData('tags'),
            'large_url' => $drawing->getMetaData('url'),
            'small_url' => greengar_get_thumbnail_link($drawing->getGUID(), 'thumb'),
            'medium_url' => greengar_get_thumbnail_link($drawing->getGUID(), 'smallthumb'),
            'avg_rating' => 3,
            'favorite' => greengar_check_favorite(get_loggedin_userid(), $drawing->getGUID(), WHITEBOARD)
        );
    }

}